<template>
  <div class="permission-management">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card class="role-card">
          <template #header>
            <div class="card-header">
              <span>角色列表</span>
            </div>
          </template>
          
          <el-input
            v-model="roleFilter"
            placeholder="搜索角色"
            prefix-icon="el-icon-search"
            clearable
            class="role-search"
          />
          
          <el-tree
            ref="roleTreeRef"
            :data="roleTreeData"
            :props="roleTreeProps"
            :filter-node-method="filterRoleNode"
            @node-click="handleRoleSelect"
            :highlight-current="true"
          />
        </el-card>
      </el-col>
      
      <el-col :span="16">
        <el-card class="permission-card">
          <template #header>
            <div class="card-header">
              <span>权限分配</span>
              <div class="header-actions">
                <el-button 
                  type="primary" 
                  @click="handleSavePermissions" 
                  :loading="saveLoading"
                  :disabled="!selectedRole"
                >
                  保存权限
                </el-button>
              </div>
            </div>
          </template>
          
          <div v-if="selectedRole" class="role-info">
            <h3>当前角色: {{ selectedRole.name }}</h3>
            <p>{{ selectedRole.description }}</p>
          </div>
          
          <div v-else class="no-role-selected">
            <el-empty description="请选择一个角色进行权限分配" />
          </div>
          
          <el-tree
            v-if="selectedRole"
            ref="permissionTreeRef"
            :data="permissionTreeData"
            show-checkbox
            node-key="id"
            :default-checked-keys="selectedRolePermissions"
            :props="permissionTreeProps"
            class="permission-tree"
          />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive, watch, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 角色搜索过滤
const roleFilter = ref('')

// 角色树引用
const roleTreeRef = ref()

// 权限树引用
const permissionTreeRef = ref()

// 保存加载状态
const saveLoading = ref(false)

// 选中的角色
const selectedRole = ref(null)

// 选中角色的权限
const selectedRolePermissions = ref([])

// 角色树数据
const roleTreeData = ref([
  {
    id: 1,
    name: '超级管理员',
    description: '拥有所有权限'
  },
  {
    id: 2,
    name: '管理员',
    description: '普通管理员权限'
  },
  {
    id: 3,
    name: '运营人员',
    description: '运营管理权限'
  },
  {
    id: 4,
    name: '客服人员',
    description: '客户服务权限'
  }
])

// 角色树属性
const roleTreeProps = {
  label: 'name',
  children: 'children'
}

// 权限树数据
const permissionTreeData = ref([
  {
    id: 1,
    label: '用户管理',
    children: [
      { id: 101, label: '用户列表' },
      { id: 102, label: '用户详情' },
      { id: 103, label: '用户编辑' },
      { id: 104, label: '用户删除' }
    ]
  },
  {
    id: 2,
    label: '角色权限',
    children: [
      { id: 201, label: '角色列表' },
      { id: 202, label: '角色详情' },
      { id: 203, label: '角色编辑' },
      { id: 204, label: '角色删除' }
    ]
  },
  {
    id: 3,
    label: '订单管理',
    children: [
      { id: 301, label: '订单列表' },
      { id: 302, label: '订单详情' },
      { id: 303, label: '订单编辑' }
    ]
  },
  {
    id: 4,
    label: '充值管理',
    children: [
      { id: 401, label: '充值列表' },
      { id: 402, label: '充值详情' }
    ]
  },
  {
    id: 5,
    label: '会员管理',
    children: [
      { id: 501, label: '会员列表' },
      { id: 502, label: '会员等级' }
    ]
  },
  {
    id: 6,
    label: '模型管理',
    children: [
      { id: 601, label: '卦象管理' },
      { id: 602, label: '算法管理' }
    ]
  },
  {
    id: 7,
    label: '系统配置',
    children: [
      { id: 701, label: '系统设置' },
      { id: 702, label: '公告管理' }
    ]
  },
  {
    id: 8,
    label: '统计分析',
    children: [
      { id: 801, label: '用户统计' },
      { id: 802, label: '业务统计' },
      { id: 803, label: '占卜统计' }
    ]
  }
])

// 权限树属性
const permissionTreeProps = {
  label: 'label',
  children: 'children'
}

// 过滤角色节点
const filterRoleNode = (value, data) => {
  if (!value) return true
  return data.name.includes(value)
}

// 监听角色搜索过滤变化
watch(roleFilter, (val) => {
  if (roleTreeRef.value) {
    roleTreeRef.value.filter(val)
  }
})

// 处理角色选择
const handleRoleSelect = (data) => {
  selectedRole.value = data
  
  // 模拟获取角色权限
  // 在实际应用中，这里应该调用API获取角色的权限
  if (data.id === 1) {
    // 超级管理员拥有所有权限
    selectedRolePermissions.value = [
      101, 102, 103, 104,
      201, 202, 203, 204,
      301, 302, 303,
      401, 402,
      501, 502,
      601, 602,
      701, 702,
      801, 802, 803
    ]
  } else if (data.id === 2) {
    // 管理员权限
    selectedRolePermissions.value = [
      101, 102, 103,
      201, 202,
      301, 302,
      401,
      501,
      601,
      701,
      801, 802
    ]
  } else if (data.id === 3) {
    // 运营人员权限
    selectedRolePermissions.value = [
      101, 102,
      301, 302,
      501,
      801, 802, 803
    ]
  } else {
    // 其他角色默认权限
    selectedRolePermissions.value = []
  }
  
  // 设置权限树的选中项
  if (permissionTreeRef.value) {
    permissionTreeRef.value.setCheckedKeys(selectedRolePermissions.value)
  }
}

// 处理保存权限
const handleSavePermissions = () => {
  if (!selectedRole.value) return
  
  saveLoading.value = true
  
  // 获取选中的权限
  const checkedKeys = permissionTreeRef.value.getCheckedKeys()
  const halfCheckedKeys = permissionTreeRef.value.getHalfCheckedKeys()
  const allCheckedKeys = [...checkedKeys, ...halfCheckedKeys]
  
  // 模拟保存权限操作
  setTimeout(() => {
    saveLoading.value = false
    ElMessage.success('权限保存成功')
    
    // 更新选中角色的权限
    selectedRolePermissions.value = allCheckedKeys
  }, 1000)
}

// 组件挂载后初始化
onMounted(() => {
  // 默认选中第一个角色
  if (roleTreeData.value.length > 0) {
    handleRoleSelect(roleTreeData.value[0])
  }
})
</script>

<style lang="scss" scoped>
.permission-management {
  .role-card {
    height: calc(100vh - 120px);
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .role-search {
      margin-bottom: 15px;
    }
  }
  
  .permission-card {
    height: calc(100vh - 120px);
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .role-info {
      margin-bottom: 20px;
      padding: 15px;
      background-color: #f5f5f5;
      border-radius: 4px;
      
      h3 {
        margin: 0 0 10px 0;
      }
      
      p {
        margin: 0;
        color: #666;
      }
    }
    
    .no-role-selected {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 300px;
    }
    
    .permission-tree {
      margin-top: 20px;
      border: 1px solid #ebeef5;
      border-radius: 4px;
      padding: 15px;
    }
  }
}
</style>